/**
 * 构建脚本树
 */
CommonUtils.regNamespace("tree", "index");

tree.index = (function(){
	//组织树节点公共参数
	var _moduleTree = {
		tags:[],
		href:"",
		nodeId:"",
		pageId:"",
		projectType:0,//项目类型 0 real monkey;1 robotium
		testType:0,//测试引擎： 0 real monkey;1 robotium;3 macaca
		eleResult:"",
		elementType:"",//普通元素类型
		webViewType:"",//webview类型
		pageName:"",//页面名称
		pageFlag:"",//插入页面标识
		cpage:1,//默认当前页为1
		pageSize:10, //默认的页面展示记录数
		numberOfPages:3, //默认显示三个记录
		totalPages:0,//默认总页面数
		initFlag:0, //标识第一次初始化元素信息展示页
		pageIdToEle:"",
		elementId:"",
		elementForPage:"",
		param:"", //拖拽时传递的参数
		moduleName:"" //选中模块的名称
	};
	//编辑项目
	var _editTreeById = function(){
		var projectid = $("#projectid").val();
		var params = {
			"projectid":projectid	
		};
		var path = contextPath+"/project/editTreeById";
		//组织树请求
		$.ajax({
			type:"POST",
			url:path,
			data:JSON.stringify(params),
			contentType:"application/json; charset=utf-8",
			dataType: "json",
	        async:false,
	        beforeSend:function(request){
	        	$.ecOverlay("正在加载中，请稍后...");
	        },
	        success:function(response){
	        	if(response){
	        		var projectList = response.projectList;
	        		var moduleList = response.moduleList;
	        		//默认组织树节点
	        		var treeList = new Array();
	        		var json = {
        				"text": "项目管理",
        				"href": "#parent"
	        		};
	        		//项目节点
	        		var fProjectList = new Array();
	        		var projectJson = {
	        				"text": projectList.project_name,
	        				"href": "#child",
	        				"tags":[projectList.project_id]
	        		};
	        		fProjectList.push(projectJson);
	        		//模块树节点组建
	        		if (moduleList!=null&&moduleList.length>0) {
	        			var modJsonList = new Array();
	        			$.each(moduleList,function(index,list){
	        				var moduleJson = {
	        						"text": list.module_name,
	        						"href": "#childNode",
	        						"tags": [projectList.project_id,list.module_id]
	        				};
	        				modJsonList.push(moduleJson);
	        			});
	        			projectJson.nodes= modJsonList;
	        		}
	        		json.nodes = fProjectList;
	        		treeList.push(json);
	        		$('#treeview').treeview({
	        	          color: "#428bca",
	        	          expandIcon: 'glyphicon glyphicon-chevron-right',
	        	          collapseIcon: 'glyphicon glyphicon-chevron-down',
	        	          nodeIcon: 'glyphicon glyphicon-bookmark',
	        	          data: treeList
	        	        });
	        	      $('#treeview').treeview('expandAll');
	        	}else{
	        		Lobibox.alert('error', {
	                    msg: "项目组织树加载失败"
	                });
	        	}
	        },error:function(response){
	            Lobibox.alert('error', {
                    msg: "请求发生错误:"+response
                });
	        }
		});
	};
	//删除子模块
	var _deleteModuleById = function(){
		if(tree.index.moduleTree.tags!=""&&tree.index.moduleTree.tags.length>1){
			var projectId = tree.index.moduleTree.tags[0];
			var moduleId = tree.index.moduleTree.tags[1];
			var params = {
				"projectId":projectId,
				"moduleId":moduleId
			};
			var path = contextPath + "/module/deleteModuleById";
			$.ajax({
				type:"POST",
				url:path,
				data:JSON.stringify(params),
				contentType:"application/json; charset=utf-8",
				dataType: "json",
		        async:false,
		        beforeSend:function(request){
		        	//弹框隐藏
	        		$("#delModuleModal").modal("hide");
		        	$.ecOverlay("正在删除中，请稍后...");
		        },
		        success:function(response){
		        	if(response){
		        		Lobibox.alert('success', {
				            msg: "模块信息删除成功"
				        });
		        		tree.index.editTreeById();
		        		//重新加载节点选中事件
		        		tree.index.nodeSelected();
		        		$("#delModule").attr("disabled",true);
		        	}
		        }
			});
		}else{
			 Lobibox.alert('warning', {
                 msg: "对不起，无法删除该模块"
             });
		}
	};
	//初始化添加模块信息
	var _initAddModuleModal = function(){
		$('#addModuleName').modal({backdrop: 'static', keyboard: false});
		$("#addModuleName").blur(function(){
			var projectId = tree.index.moduleTree.tags[0];
			var moduleName = $("#addModuleName").val();
			var params = {
				"moduleName":moduleName,
				"projectId":projectId
			};
			var path = contextPath + "/module/checkModuleName";
			$.ajax({
				type:"POST",
				url:path,
				data:JSON.stringify(params),
				contentType:"application/json; charset=utf-8",
				dataType: "json",
		        async:false,
		        success:function(response){
		        	if(response&&response.code==-1){
		        		$("#saveModuleInfo").attr("disabled",true);
		        		alert("模块名称不能相同，请重新填写模块名称！")
		        	}else{
		        		$("#saveModuleInfo").attr("disabled",false);
		        	}
		        }
			});
		});
	};
	//增加子模块
	var _insertModule = function(){
		if(tree.index.moduleTree.href=="#child"){
			//获取模块名称
			var moduleName = $("#addModuleName").val();
			var moduleLoginUrl = $("#addModuleUrl").val();
			var moduleOrder = $("#addModuleOrder").val();
			var projectId = tree.index.moduleTree.tags[0];			
			var params = {
				"moduleName":moduleName,
				"projectId":projectId,
				"moduleLoginUrl":moduleLoginUrl,
				"moduleOrder":moduleOrder
			};
			var path = contextPath + "/module/insertModule";
			$.ajax({
				type:"POST",
				url:path,
				data:JSON.stringify(params),
				contentType:"application/json; charset=utf-8",
				dataType: "json",
		        async:false,
		        beforeSend:function(request){
		        	//弹框隐藏
	        		$("#addModuleModal").modal("hide");
		        	$.ecOverlay("正在添加中，请稍后...");
		        },
		        success:function(response){
		        	if(response){
		        		Lobibox.alert('success', {
				            msg: "模块信息添加成功"
				        });
		        		//重新加载组织树
		        		tree.index.editTreeById();
		        		//重新加载初始化节点选中事件
		        		$('#treeview').treeview('selectNode', [ tree.index.moduleTree.nodeId, { silent: true } ]);
		        		tree.index.nodeSelected();
		        	}
		        }
			});
		}else{
			Lobibox.alert('warning', {
                msg: "对不起，无法新增模块"
            });
		}
	};
	//初始化模块信息
	var _initModifyModuleModal = function(){
		if(tree.index.moduleTree.href=="#childNode"){
			//获取模块名称
			var projectId = tree.index.moduleTree.tags[0];
			var moduleId = tree.index.moduleTree.tags[1];
			var params = {
				"projectId":projectId,
				"moduleId":moduleId
			};
			var path = contextPath + "/module/queryModuleById";
			$.ajax({
				type:"POST",
				url:path,
				data:JSON.stringify(params),
				contentType:"application/json; charset=utf-8",
				dataType: "json",
		        async:false,
		        success:function(response){
		        	if(response){
		        		//初始化模块
		        		if(response.module){
		        			$("#modifyModuleName").val(response.module.moduleName);
			        		$("#modifyModuleOrder").val(response.module.moduleOrder);
			        		$("#modifyModuleUrl").val(response.module.moduleLoginUrl);
		        		}
		        	}
		        }
			});
		}
	}
	//修改根模块信息
	var _modifyModuleInfo = function(){
		if(tree.index.moduleTree.href=="#childNode"){
			//获取模块名称
			var moduleName = $("#modifyModuleName").val();
			var modifyModuleUrl = $("#modifyModuleUrl").val();
			var modifyModuleOrder = $("#modifyModuleOrder").val();
			var projectId = tree.index.moduleTree.tags[0];
			var moduleId = tree.index.moduleTree.tags[1];
			var params = {
				"moduleName":moduleName,
				"projectId":projectId,
				"moduleId":moduleId,
				"modifyModuleUrl":modifyModuleUrl,
				"modifyModuleOrder":modifyModuleOrder
			};
			var path = contextPath + "/module/updateModuleById";
			$.ajax({
				type:"POST",
				url:path,
				data:JSON.stringify(params),
				contentType:"application/json; charset=utf-8",
				dataType: "json",
		        async:false,
		        beforeSend:function(request){
		        	//弹框隐藏
	        		$("#modifyModuleModal").modal("hide");
		        	$.ecOverlay("正在修改中，请稍后...");
		        },
		        success:function(response){
		        	if(response){
		        		Lobibox.alert('success', {
				            msg: "模块信息修改成功"
				        });
		        		//重新加载组织树
		        		tree.index.editTreeById();
		        		//重新加载初始化节点选中事件
		        		$('#treeview').treeview('selectNode', [ tree.index.moduleTree.nodeId, { silent: true } ]);
		        		tree.index.nodeSelected();
		        		//重新加载脚本结构图
		        		tree.index.showScriptChart();
		        	}
		        }
			});
		}else{
			Lobibox.alert('warning', {
                msg: "对不起，无法修改模块信息"
            });
		}
	};
	//下载脚本
	var _loadProjectScript = function(){
		
	};
	var _showScriptChart = function(){
		var num=0;
		var coverpagename;
		//获取模块id
		var moduleId = "";
		var projectId ="";
		if(tree.index.moduleTree.tags&&tree.index.moduleTree.tags.length>0){
			projectId = tree.index.moduleTree.tags[0];
			moduleId= tree.index.moduleTree.tags[1];
			$("#moduleId").val(moduleId);
		}else{
			alert("模块标识ID为空");
			return;
		}
		//返回和生成脚本事件
		/*$("#backToProject").bind("click",function(){
			window.location.href = contextPath + "/user/home";
		});*/
		/*$("#buildScript").bind("click",function(){
			tree.index.buildScript();
		});*/
		var params = {
			"moduleId":moduleId,
			"projectId":projectId
		};
		var path=contextPath+"/module/showScriptChart";
		$.ajax({
	    	type:"POST",
	    	url:path,
	    	contentType:"application/json; charset=utf-8",
	    	data:JSON.stringify(params),
	    	dataType:"json",
	        success: function (response) {
	        	var items = [];
	        	var allPages = "";
	        	if(response.pageList&&response.pageList.length==0){
	        		allPages=TAFFY(response.defaultPage);
	        	}else{
	        		allPages=TAFFY(response.pageList);
	        	}
	        	allPages({"parent_page_id":-1}).each(function (record,recordnumber) {
	        	     loops(record);
	             });
	        	//start loop the json and form the html
	        	function loops(root){
	        		if (root.parent_page_id == -1){
	        		   items.push("<li class='root unic"+root.page_id+" roots' id='" + root.page_name + "'><span class='label_node'><a id='"+ root.page_id+"' title='"+root.page_name+"' href='#'>" + root.page_name+"</a></br></span>");
	        		}else{
	        		   items.push("<li class='child unic"+root.page_id+"roots' id='" + root.page_name + "'><span class='label_node'><a id='"+ root.page_id+"' href='#' title='"+root.page_name+",页面层级为 "+root.page_depth+"' onclick='tree.index.showElements(this,this.id);'>" + root.page_name +"</a></br></span>");
	        		}
	        		var c = allPages({"parent_page_id":root.page_id}).count();
	        		if (c != 0){
	        		  items.push("<ul>");
	        		  allPages({"parent_page_id":root.page_id}).each(function (record,recordnumber) {
	        			loops(record);
	        		  });
	        		 items.push("</ul></li>");
	        		}else{
	        		  items.push("</li>");
	        		}
	        	};// End the generate html code
	        	//push to html code
	        	//删除原来存在的html
	        	if($("#org")&&$("#org").length>0){
	        		$("#org").remove();
	        	}
	        	$( "<ul/>", {
	        	"id": "org",
	        	"style" : "float:right;",
	        	html: items.join("")
	        	}).appendTo("body");
	        	$("#moduleId").val("");
	        	$("#moduleId").val(moduleId);//programid
	        	tree.index.initTree();
	          }
	    });
		//setTimeout(tree.index.initTree(),8000);
		//初始化脚本结构图功能事件
	    var add_to_node, del_node, classList,parentid,programid,add_to_node_class;
		var regx = /\w*(row)/;
		if ($('#upload-chart').length == 0) {
		   $("body").append("<ul id='upload-chart'></ul>");
		}
	    tree.index.scroll();
	    $("input[name='webview']").click(function(){
	        if($("input[name='webview']").attr("checked")=="checked"){
	        	$("#classtype").html("");
	            $("#type").html("");
	            $("input[name='webview']").val(1);
	          }else{
	        	  $("#classtype").html("");
	              $("#type").html("");
	              $("input[name='webview']").val(0);
	          }
		    
	        });
	    $("input[name='dragWebView']").click(function(){
	        if($("input[name='dragWebView']").attr("checked")=="checked"){
	        	$("#classtype").html("");
	            $("#type").html("");
	            $("input[name='dragWebView']").val(1);
	          }else{
	        	  $("#classtype").html("");
	              $("#type").html("");
	              $("input[name='dragWebView']").val(0);
	          }
	        });
	};
	//生成脚本
	var _buildScript = function(){
		$("#buildScriptModal").modal("hide");
		var projectId = $("#projectid").val();
		var moduleId = $("#buildScriptModel").find("option:selected").val();
		var scriptName= $("#scriptName").val();
		if(scriptName==null||scriptName==""){
			return;
		}
		var scriptVersion= $("#scriptVersion").val();
		
		var params = {
				"projectId":projectId,
				"moduleId":moduleId,
				"scriptName":scriptName,
				"scriptVersion":scriptVersion
			};
			var path = contextPath + "/project/buildScript";
			$.ajax({
				type:"POST",
				url:path,
				contentType:"application/json; charset=utf-8",
				data:JSON.stringify(params),
				dataType:"json",
				async:false,
		        beforeSend:function(request){
		        	$.ecOverlay("正在加载中，请稍后...");
		        },
				success: function (response) {
					if(response&&response.code=="0"){
						Lobibox.alert('success', {
				            msg: "生成项目脚本成功"
				        });
						//跳转到项目列表页
						//window.location.href = contextPath +"/user/home";
					}else if(response.code=="-2"){
						Lobibox.alert('warning', {
			                 msg: "对不起，请重新登录！"
			             });
						window.location.href = contextPath + "/user/index";
					}else{
						Lobibox.alert('error', {
		                    msg: "生成项目脚本失败"
		                });
					}
					//关闭添加页面
					//$("#modifyPage").modal("hide");
					//重新加载组织结构图
					//tree.index.showScriptChart();
		        },error:function(response){
			        Lobibox.alert('error', {
	                    msg: "生成项目脚本异常"
	                });
			    }
			});
	};
	//下载脚本
	var _downLoadScript = function(){
		var projectId = $("#projectid").val();
		var params = {
				"projectId":projectId
			};
			var path = contextPath + "/project/downLoadScript";
			$.ajax({
				type:"POST",
				url:path,
				contentType:"application/json; charset=utf-8",
				data:JSON.stringify(params),
				dataType:"json",
				async:false,
				success: function (response) {
					if(response&&response.code=="0"){
						window.location.href = response.url;
						/*Lobibox.alert('success', {
				            msg: "下载项目脚本成功"
				        });*/
					}else if(response.code=="-1"){
						Lobibox.alert('warning', {
			                 msg: response.message
			             });
					}else{
						Lobibox.alert('error', {
		                    msg: "下载项目脚本失败"
		                });
					}
		        },error:function(response){
			        Lobibox.alert('error', {
	                    msg: "下载项目脚本异常"
	                });
			    }
			});
	}
	//初始化修改页面
	var _initModifyPage = function(pthis){
		if($(pthis)&&$(pthis).parent().parent().find("span a")){
			tree.index.moduleTree.pageId = $(pthis).parent().parent().find("span a").attr("id");
			//获取该页面的名称
			tree.index.moduleTree.pageName = $(pthis).parent().parent().find("span a").text();
		}
		if(tree.index.moduleTree.pageName!=""&&tree.index.moduleTree.pageName!=null){
			$("#modifyThePageName").val(tree.index.moduleTree.pageName);
		}
		//修改页面名称校验事件
		$("#modifyThePageName").bind("blur",function(){
			if(!$("#modifyPageNameSpan").hasClass("span-hidden")){
				$("#modifyPageNameSpan").addClass("span-hidden");
				$("#modifyPageButton").attr("disabled",false);
			}
			var pageName = $("#modifyThePageName").val();
			if(pageName==""||pageName==null){
				//alert("请输入页面名称");
				return;
			}
			var flag = tree.index.checkModifyPageName(pageName);
			if(flag){
				$("#modifyPageNameSpan").removeClass("span-hidden");
				$("#modifyPageButton").attr("disabled",true);
			}
		});
		
		console.log("--------------");
	};
	//修改页面名称提交
	var _modifyPage = function(){
		var pageName = $("#modifyThePageName").val();
		if(tree.index.moduleTree.pageName==pageName||pageName==""){
			Lobibox.alert('warning', {
                msg: "您输入的页面名称为空或者与之前名称一致，无法修改"
            });
			return;
		}
		var params = {
			"pageId":tree.index.moduleTree.pageId,
			"pageName":pageName
		};
		var path = contextPath + "/page/modifyPage";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				Lobibox.alert('success', {
		            msg: "修改页面成功"
		        });
				//关闭添加页面
				$("#modifyPage").modal("hide");
				//重新加载组织结构图
				tree.index.showScriptChart();
	        },error:function(response){
		        Lobibox.alert('error', {
                    msg: "修改页面失败"
                });
		    }
		});
	};
	//展示页面元素信息（包括分页处理）
	var _showElements = function(pthis,pageId){
		//默认请求
		tree.index.moduleTree.pageId = pageId;
		if(pthis&&pthis!=""){
			tree.index.moduleTree.pageName = $(pthis).text();
		}
		$.fn.bootstrapPaginator.defaults.flag = "1";//标识是哪个功能的分页
		var params = {
			"pageId":pageId,
			"cpage":tree.index.moduleTree.cpage,
			"pageSize":tree.index.moduleTree.pageSize
		};
		var path = contextPath + "/element/showElements";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"html",
			async:true,
			success: function (response) {
				//展示页面元素信息
				$("#chart").html(response);
				//先隐藏所有的操作按钮
	    		$(".postTitle span").removeClass("span-hidden");
	    		//隐藏返回项目信息列表按钮和生成脚本按钮
	    		$("#backToProject").addClass("span-hidden");
	    		$("#downLoadScript").addClass("span-hidden");
	    		$("#buildScript").addClass("span-hidden");
	    		//将元素页面相应的操作按钮展示
	    		$("#backToPage").bind("click",function(){
	    			tree.index.showScriptChart();
	    			$(".postTitle span").addClass("span-hidden");
	    			//隐藏返回项目信息列表按钮和生成脚本按钮
	    			$("#backToProject").removeClass("span-hidden");
	    			$("#buildScript").removeClass("span-hidden");
	    			$("#downLoadScript").removeClass("span-hidden");
	    			//tree.index.moduleTree.initFlag==0;//返回到脚本结构图，需要把标识置成初始化状态0
	    			//将H1中的信息提示文本修改
		    		$("#cb_post_title_url").text("构建项目结构图");
	    		});
	    		//将H1中的信息提示文本修改
	    		$("#cb_post_title_url").text("项目页面元素信息列表");
	    		/*if(tree.index.moduleTree.initFlag==0){
	    			//元素页面操作
	    			$("#modifyElement").bind("click",function(){
						tree.index.initModifyElement();
					});
					$("#addElement").bind("click",function(){
						tree.index.initAddElement();  
					});
	    		}*/
				//展示分页功能
				var currentPage = tree.index.moduleTree.cpage;
				if(tree.index.moduleTree.totalPages>0){
					var options = {
			                size:"large",
			                bootstrapMajorVersion:3,
			                currentPage: currentPage,
			                numberOfPages: tree.index.moduleTree.numberOfPages,
			                totalPages:tree.index.moduleTree.totalPages
			            };
			            var element = $('#bp-3-element-lg-test');
			            element.bootstrapPaginator(options);
				}
	            //将遮罩层去掉
	            $.unecOverlay();
	        },error:function(response){
	        	Lobibox.alert('error', {
                    msg: "查询页面元素失败"
                });
		    }
		});
		
	};
	//新增元素页面初始化
	var _initAddElement = function(){
		$("#addElePageName").val(tree.index.moduleTree.pageName);
		//查询元素类型和操作类型，再初始化
		tree.index.queryElementType();
		if(tree.index.moduleTree.projectType==0){
			$("#addCheckElementType").hide();
			$("#addWebView").hide();
		}
		var $typeOps = "<option value=''>请选择元素类型</option>";
		var $viewTypeOps = "<option value=''>请选择元素类型</option>";
		var $actionOps = "<option value=''>请选择操作类型</option>";
		var $expectOps = "<option value=''>请选择期待元素类型</option>";
		var $expectTypeOps = "<option value=''>请选择校验的类型</option>";
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elTypeList&&tree.index.moduleTree.eleResult.elTypeList.length>0){
			$.each(tree.index.moduleTree.eleResult.elTypeList,function(index,elList){
				if(elList.element_type_flag==0){
					$typeOps = $typeOps+"<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
				}else{
					$viewTypeOps = $viewTypeOps +"<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
				}
			});
		}
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elActionList&&tree.index.moduleTree.eleResult.elActionList.length>0){
			$.each(tree.index.moduleTree.eleResult.elActionList,function(index,acList){
				$actionOps = $actionOps+"<option value='"+acList.element_action_id+"'>"+acList.element_action_name+"</option>";
			});
		}
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elExpectList&&tree.index.moduleTree.eleResult.elExpectList.length>0){
			$.each(tree.index.moduleTree.eleResult.elExpectList,function(index,ecList){
				$expectOps = $expectOps+"<option value='"+ecList.elementExpectName+"'>"+ecList.elementExpectName+"</option>";
			});
		}
		//校验的类型
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.expectTypes&&tree.index.moduleTree.eleResult.expectTypes.length>0){
			$.each(tree.index.moduleTree.eleResult.expectTypes,function(index,type){
				$expectTypeOps = $expectTypeOps+"<option value='"+type.expectTypeName+"'>"+type.expectTypeName+"</option>";
			});
		}
		tree.index.moduleTree.elementType = $typeOps;//普通元素类型
		tree.index.moduleTree.webViewType = $viewTypeOps;//webview类型
		//判断如果是Macaca项目，则初始化macaca元素类型
		if(tree.index.moduleTree.testType==3){
			$("#addCheckElementType").prop("checked",true);
			$("#addElementType").html($viewTypeOps);
		}else if(tree.index.moduleTree.testType==4){
			//操作元素类型初始化
			$("#addCheckElementType").prop("checked",true);
			$("#addElementType").html($viewTypeOps);
			$("#addElementFocusType").html($viewTypeOps);
			//iframe定位初始化
			$("#addMoreIFrame").removeClass("template-hidden");
			$("#addIFrameElementType").html($viewTypeOps);
		}else{
			$("#addElementType").html($typeOps);
		}
		$("#addElementAction").html($actionOps);
		$("#addElementExpectName").html($expectOps);
		$("#addElementExpectType").html($expectTypeOps);
		//查询一个页面上的所有元素信息
		var $preEles = "<option value=''>请选择前置元素</option>";;
		var params = {
				"pageId":tree.index.moduleTree.pageId
			};
		var path = contextPath + "/element/queryElementById";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.code=="0"){
					$.each(response.elementList,function(index,list){
						//判断元素id是否已被选择为前置元素
						var elementId = list.element_id;
						var elementContent = list.element_content;//元素的id
						var elementOperateDesc = list.element_operate_desc;
						var i = 0;
						var flag = false;
						while(i<response.elementList.length){
							if(response.elementList[i].element_precious_id==elementId){
								flag = true;
								return;
							}
							i++;
						}
						if(!flag){
							if(elementOperateDesc!=null&&elementOperateDesc!=""){
								$preEles = $preEles + "<option value='"+elementId+"'>"+elementId+"-"+elementOperateDesc+"</option>";
							}else{
								$preEles = $preEles + "<option value='"+elementId+"'>"+elementId+"</option>";
							}
						}
					});
					$("#addElementPrecious").html($preEles);
				}
	        },error:function(response){
		        Lobibox.alert('error', {
                    msg: "查询元素异常"
                });
		    }
		});
		//webView类型的checkBox事件
		$("#addCheckElementType").bind("click", function () {
            if($("input[type='checkbox']").is(':checked')){
            	$("#addElementType").html(tree.index.moduleTree.webViewType);
            }else{
            	$("#addElementType").html(tree.index.moduleTree.elementType);
            }
        });
		//初始化是否需要切换IFrame
		var iFrameVal = 0;
		$("#addIsUserIFrame input:radio").click(function(){
			iFrameVal=$(this).val();
    		if(iFrameVal==0){
    			$("#addIFrameContent").addClass("template-hidden");
    		}else if(iFrameVal==2){
    			//隐藏所有的元素信息
    			$("#addIFrameContent").removeClass("template-hidden");
    			//$("#moreElementPage").addClass("element-hidden");
    		}
    	});
		//元素操作类型事件绑定
		$('#addElementAction').change(function(){ 
			var text = $("#addElementAction").find("option:selected").text();
			if(text=="input"||text=="dragForView"||text=="select"){
				$("#addElementTextId").removeClass("elementTextDiv");
				$("#addElementDragId").addClass("dragElementDiv");
				$("#addElementFocusTypeId").addClass("elementTextDiv");
			}else if(text=="drag-left"||text=="drag-right"||text=="drag-up"||text=="drag-down"||text.startsWith("scroll")){
				$("#addElementDragId").removeClass("dragElementDiv");
				$("#addElementTextId").addClass("elementTextDiv");
				$("#addElementFocusTypeId").addClass("elementTextDiv");
			}else if(text=="focusClick"){
				$("#addElementFocusTypeId").removeClass("elementTextDiv");
				$("#addElementTextId").removeClass("elementTextDiv");
				$("#addElementDragId").addClass("dragElementDiv");
			}else{
				$("#addElementFocusTypeId").addClass("elementTextDiv");
				$("#addElementTextId").addClass("elementTextDiv");
				$("#addElementDragId").addClass("dragElementDiv");
			};
		});
	};
	//修改元素页面初始化
	var _initModifyElement = function(pageId,elementId,elementForPage){
		//判断是否选择了需要修改的元素项
		/*$inputs = $("#elmentTbody tr td").find(":checked");
		if($inputs&&$inputs.length==0){
			alert("请选择您要修改的元素信息");
			$("#modifyElementModal").modal("hide");
			return;
		}
		if($inputs&&$inputs.length>1){
			alert("不能选择多项元素信息修改");
			$("#modifyElementModal").modal("hide");
			return;
		}*/
		//展示修改元素modal框
		$("#modifyElementModal").modal('show');
		if(tree.index.moduleTree.testType==4){
			//将iframe的div先隐藏
			$("#modifyMoreIFrame").removeClass("template-hidden");
			//$("#modifyIFrameContent").addClass("template-hidden");
			//初始化是否需要切换IFrame
			var iFrameVal = 0;
			$("#modifyIsUserIFrame input:radio").click(function(){
				iFrameVal=$(this).val();
	    		if(iFrameVal==0){
	    			$("#modifyIFrameContent").addClass("template-hidden");
	    		}else if(iFrameVal==2){
	    			//隐藏所有的元素信息
	    			$("#modifyIFrameContent").removeClass("template-hidden");
	    			//$("#moreElementPage").addClass("element-hidden");
	    		}
	    	});
		}
		//获取页面pageId和元素id
		/*var pageId = $($inputs).attr("name");
		var elementId = $($inputs).attr("id");*/
		tree.index.moduleTree.pageIdToEle = pageId;
		tree.index.moduleTree.elementId = elementId;
		tree.index.moduleTree.elementForPage =elementForPage;
		$("#modifyElePageName").val(tree.index.moduleTree.pageName);
		//查询元素类型和操作类型，再初始化
		tree.index.queryElementType();
		if(tree.index.moduleTree.projectType==0){
			$("#modifyCheckElementType").hide();
			$("#modifyWebView").hide();
		}
		var $typeOps = "<option value=''>请选择元素类型</option>";
		var $viewTypeOps = "<option value=''>请选择元素类型</option>";
		var $actionOps = "<option value=''>请选择操作类型</option>";
		var $iframeOps = "<option value=''>请选择元素类型</option>";
		var $expectOps = "<option value=''>请选择期待元素类型</option>";
		var $expectTypeOps = "<option value=''>请选择校验的类型</option>";
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elTypeList&&tree.index.moduleTree.eleResult.elTypeList.length>0){
			$.each(tree.index.moduleTree.eleResult.elTypeList,function(index,elList){
				if(elList.element_type_flag==0){
					$typeOps = $typeOps+"<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
				}else{
					$viewTypeOps = $viewTypeOps +"<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
				}
			});
		}
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elActionList&&tree.index.moduleTree.eleResult.elActionList.length>0){
			$.each(tree.index.moduleTree.eleResult.elActionList,function(index,acList){
				$actionOps = $actionOps+"<option value='"+acList.element_action_id+"'>"+acList.element_action_name+"</option>";
			});
		}
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elExpectList&&tree.index.moduleTree.eleResult.elExpectList.length>0){
			$.each(tree.index.moduleTree.eleResult.elExpectList,function(index,ecList){
				$expectOps = $expectOps+"<option value='"+ecList.elementExpectName+"'>"+ecList.elementExpectName+"</option>";
			});
		}
		//校验的类型
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.expectTypes&&tree.index.moduleTree.eleResult.expectTypes.length>0){
			$.each(tree.index.moduleTree.eleResult.expectTypes,function(index,type){
				$expectTypeOps = $expectTypeOps+"<option value='"+type.expectTypeName+"'>"+type.expectTypeName+"</option>";
			});
		}
		tree.index.moduleTree.elementType = $typeOps;//普通元素类型
		tree.index.moduleTree.webViewType = $viewTypeOps;//webview类型
		//判断如果是Macaca项目，则初始化macaca元素类型
		if(tree.index.moduleTree.testType==3){
			$("#modifyCheckElementType").prop("checked",true);
			$("#modifyElementType").html($viewTypeOps);
		}else if(tree.index.moduleTree.testType==4){
			//初始化定位iframe类型
			$("#modifyElementFocusType").html($viewTypeOps);
			$("#modifyIFrameElementType").html($viewTypeOps);
		}else{
			$("#modifyElementType").html($typeOps);
		}
		$("#modifyElementAction").html($actionOps);
		$("#modifyElementExpectName").html($expectOps);
		$("#modifyElementExpectType").html($expectTypeOps);
		//查询一个页面上的所有元素信息,展示前置元素信息
		var $preEles = "<option value=''>请选择前置元素</option>";
		var params = {
				"pageId":tree.index.moduleTree.pageId
			};
		var path = contextPath + "/element/queryElementById";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.code=="0"){
					$.each(response.elementList,function(index,list){
						//判断元素id是否已被选择为前置元素
						var element_id = list.element_id;
						var elementContent = list.element_content;
						var elementOperateDesc = list.element_operate_desc;
						var i = 0;
						var flag = false;
						while(i<response.elementList.length){
							if(response.elementList[i].element_precious_id==element_id||element_id==elementId){
								flag = true;
								return;
							}
							i++;
						}
						if(!flag){
							if(elementOperateDesc!=null&&elementOperateDesc!=""){
								$preEles = $preEles + "<option value='"+element_id+"'>"+element_id+"-"+elementOperateDesc+"</option>";
							}else{
								$preEles = $preEles + "<option value='"+element_id+"'>"+element_id+"</option>";
							}
						}
					});
					$("#modifyElementPrecious").html($preEles);
				}
	        },error:function(response){
		        Lobibox.alert('error', {
                    msg: "查询元素异常"
                });
		    }
		});
		//webView类型的checkBox事件
		/*$("#modifyCheckElementType").bind("click", function () {
            if($("#modifyCheckElementType").is(':checked')){
            	$("#modifyElementType").html(tree.index.moduleTree.webViewType);
            }else{
            	$("#modifyElementType").html(tree.index.moduleTree.elementType);
            }
        });*/
		//元素操作类型事件绑定
		$("#modifyElementAction").change(function(){
			var text = $("#modifyElementAction").find("option:selected").text();
			if(text=="input"||text=="dragForView"){
				$("#modifyElementTextId").removeClass("elementTextDiv");
				$("#modifyElementDragId").addClass("dragElementDiv");
				$("#modifyElementFocusTypeId").addClass("elementTextDiv");
			}else if(text=="drag-left"||text=="drag-right"||text=="drag-up"||text=="drag-down"||text.startsWith("scroll")){
				$("#modifyElementDragId").removeClass("dragElementDiv");
				$("#modifyElementTextId").addClass("elementTextDiv");
				$("#modifyElementFocusTypeId").addClass("elementTextDiv");
			}else if(text=="focusClick"){
				$("#modifyElementFocusTypeId").removeClass("elementTextDiv");
				$("#modifyElementTextId").removeClass("elementTextDiv");
				$("#modifyElementDragId").addClass("dragElementDiv");
			}else{
				$("#modifyElementFocusTypeId").addClass("elementTextDiv");
				$("#modifyElementTextId").addClass("elementTextDiv");
				$("#modifyElementDragId").addClass("dragElementDiv");
			};
		});
		//初始化修改页面的元素信息
		var params = {
			"pageId":pageId,
			"elementId":elementId
		};
		var path = contextPath + "/element/queryElementById";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.code==0){
					//查询元素信息成功
					if(response.elementList.length>0){
						$.each(response.elementList,function(index,list){
							var $typeOps = $("#modifyElementType").find("option");
							//如果初始化的elementType中存在
							var isFlag = false;//标识是否在初始化的元素类型中就存在
							$.each($typeOps,function(index,$typeOp){
								if($($typeOp).val()==list.element_type_id){
									$($typeOp).attr("selected",true);
									isFlag = true;
									return false;
								}
							});
							//聚焦操作
							var $focusTypeOps = $("#modifyElementFocusType").find("option");
							$.each($focusTypeOps,function(index,$focusOps){
								if($($focusOps).val()==list.focus_type_id){
									$($focusOps).attr("selected",true);
									//isFlag = true;
									return false;
								}
							});
							//若不存在，则选中checkbox，替换成Webview的数据项
							if(!isFlag){
								$("#modifyCheckElementType").click();
								$typeOps = $("#modifyElementType").find("option");
								$.each($typeOps,function(index,$typeOp){
									if($($typeOp).val()==list.element_type_id){
										$($typeOp).attr("selected",true);
										return false;
									}
								});
							}
							if(list.over_iframe_flag=="2"){
								$("#modifyOverIFrameRadio").prop("checked",true);
							}
							if(list.is_switch_page=="A"){
								$("#modifySwitchPageId").prop("checked",true);
							}
							//判断是否有iframe
							if(list.is_iframe_flag=="2"){
								$("#modifyMoreIFrame").removeClass("template-hidden");
								//选中radio
								$("#modifyIFrameRadioId").prop("checked",true);
								$("#modifyIFrameContent").removeClass("template-hidden");
								var $iframeTypes = $("#modifyIFrameElementType").find("option");
								$.each($iframeTypes,function(index,$iframeType){
									if($($iframeType).val()==list.iframe_element_type){
										$($iframeType).attr("selected",true);
										return false;
									}
								});
								$("#modifyIFrameElementContent").val(list.iframe_element_content);
							}
							$("#modifyElementContent").val(list.element_content);	
							$("#modifyElementOrder").val(list.element_order);
							$("#modifyElementWaitTime").val(list.element_wait_time);
							var $actionOps = $("#modifyElementAction").find("option");
							$.each($actionOps,function(index,$actionOp){
								if($($actionOp).text()==list.element_action_name){
									$($actionOp).attr("selected",true);
									//判断如果是input类型，需要展示输入内容框
									if(list.element_action_name=="input"||list.element_action_name=="select"){
										$("#modifyElementTextId").removeClass("elementTextDiv");
										$("#modifyElementDragId").addClass("dragElementDiv");
									}
									if(list.element_action_name=="focusClick"){
										$("#modifyElementTextId").removeClass("elementTextDiv");
										$("#modifyElementFocusTypeId").removeClass("elementTextDiv");
										$("#modifyElementDragId").addClass("dragElementDiv");
									}
									if(list.element_action_name=="click"){
										$("#modifyElementTextId").addClass("elementTextDiv");
										$("#modifyElementText").val("");
										$("#modifyElementDragId").addClass("dragElementDiv");
										$("#modifyElementDrag").val("");
									}
									if(list.element_action_name=="drag-left"||list.element_action_name=="drag-right"||list.element_action_name=="drag-up"||list.element_action_name=="drag-down"||list.element_action_name.startsWith("scroll")){
										$("#modifyElementDragId").removeClass("dragElementDiv");
										$("#modifyElementTextId").addClass("elementTextDiv");
									}
									return false;
								}
							});
							var $expectOps = $("#modifyElementExpectName").find("option");
							$.each($expectOps,function(index,$expectOp){
								if($($expectOp).text()==list.element_expect_name){
									$($expectOp).attr("selected",true);
									return false;
								}
							});
							if(list.element_text!=null&&list.element_text!=""){
								$("#modifyElementText").val(list.element_text);
							}
							if(list.element_text_flag!=null&&list.element_text_flag!=""&&list.element_text_flag=="Y"){
								$("#modifyRepeatContentFlag").prop("checked",true);
							}
							if(list.drag_count!=null&&list.drag_count!=""){
								$("#modifyElementDrag").val(list.drag_count);
							}
							if(list.element_expect_name!=null&&list.element_expect_name!=""){
								$("#modifyElementExpectName").val(list.element_expect_name);
							}
							if(list.element_expect_content!=null&&list.element_expect_content!=""){
								$("#modifyElementExpectContent").val(list.element_expect_content);
							}
							//if(list.element_expect_order!=null&&list.element_expect_order!=""){
								$("#modifyElementExpectOrder").val(list.element_expect_order);
							//}
							if(list.element_operate_desc!=null&&list.element_operate_desc!=""){
								$("#modifyElementOperateDesc").val(list.element_operate_desc);
							}
							if(list.element_expect_result!=null&&list.element_expect_result!=""){
								$("#modifyElementExpectResult").val(list.element_expect_result);
							}
							//初始化前置元素
							$("#modifyElementPrecious").append("<option value='"+list.element_precious_id+"'>"+list.element_precious_id+"</option>");
							var $elePres = $("#modifyElementPrecious").find("option");
							$.each($elePres,function(index,$elePre){
								if($($elePre).val()==list.element_precious_id){
									$($elePre).attr("selected",true);
									return false;
								}
							});
							//初始化校验内容
							var $expectTypes = $("#modifyElementExpectType").find("option");
							$.each($expectTypes,function(index,$expectType){
								if($($expectType).val()==list.element_expect_type){
									$($expectType).attr("selected",true);
									return false;
								}
							});
						});
					}else{
						Lobibox.alert('warning', {
			                 msg: "没有查询到元素信息"
			             });
						$("#modifyElementModal").modal("hide");
					}
				}else if(response.code=="-2"){
					Lobibox.alert('warning', {
		                 msg: "对不起，请重新登录！"
		             });
					$("#modifyElementModal").modal("hide");
					window.location.href = contextPath + "/user/index";
				}else{
					alert("查询页面元素失败");
					$("#modifyElementModal").modal("hide");
				}
			},error:function(response){
		        Lobibox.alert('error', {
                    msg: "查询页面元素异常"
                });
		        $("#modifyElementModal").modal("hide");
		    }
		});
	};
	//修改元素界面元素类型
	var _modifyElementType = function(){
		if($("#modifyCheckElementType").is(':checked')){
        	$("#modifyElementType").html(tree.index.moduleTree.webViewType);
        }else{
        	$("#modifyElementType").html(tree.index.moduleTree.elementType);
        }
	};
	//删除页面元素初始化
	var _initDelElement = function(pageId,elementId,elementForPage){
		/*var $inputs = $("#elmentTbody tr td").find(":checked");
		if($inputs&&$inputs.length==0){
			alert("请选择需要删除的元素信息");
			//$("#delElementModal").modal("hide");
			return;
		}
		var elementForPage = "";
		$.each($inputs,function(index,$input){
			elementForPage = $($input).attr("value");
		});*/
		tree.index.moduleTree.pageIdToEle = pageId;
		tree.index.moduleTree.elementId = elementId;
		tree.index.moduleTree.elementForPage = elementForPage;
		if(elementForPage=="Y"){
			$("#delElementInfo").text('删除该页面元素，将同时删除对应页面的所有信息？');
			//展示删除的modal框
			$("#delElementModal").modal('show');
		}else{
			$("#delElementInfo").text('确定删除元素？');
			//展示删除的modal框
			$("#delElementModal").modal('show');
		}
	};
	//删除页面元素
	var _delElement = function(){
		/*var $inputs = $("#elmentTbody tr td").find(":checked");
		if($inputs&&$inputs.length==0){
			alert("请选择需要删除的元素信息");
			//$("#delElementModal").modal("hide");
			return;
		}
		var elements= new Array();
		$.each($inputs,function(index,$input){
			var param = {};
			param.pageId = $($input).attr("name");
			param.elementId = $($input).attr("id");
			param.elementForPage= $($input).attr("value");
			elements.push(param);
		});*/
		var params = {
			"pageId":tree.index.moduleTree.pageIdToEle,
			"elementId":tree.index.moduleTree.elementId,
			"elementForPage":tree.index.moduleTree.elementForPage
		};
		var path = contextPath+"/element/delElement";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.code==0){
					Lobibox.alert('success', {
			            msg: "删除页面元素成功"
			        });
					//将删除框隐藏
					$("#delElementModal").modal("hide");
					//重新刷新元素展示页记录
					tree.index.showElements("",tree.index.moduleTree.pageId);
				}else if(response.code=="-2"){
					alert("对不起，请重新登录！");
					$("#delElementModal").modal("hide");
					window.location.href = contextPath + "/user/index";
				}else{
					Lobibox.alert('error', {
	                    msg: "删除页面元素失败"
	                });
					$("#delElementModal").modal("hide");
				}
			},error:function(response){
				Lobibox.alert('error', {
                    msg: "删除页面元素异常"
                });
		        $("#delElementModal").modal("hide");
		    }
		});
	};
	//修改页面元素
	var _modifyElement = function(){
		/*$inputs = $("#elmentTbody tr td").find(":checked");
		if($inputs&&$inputs.length>1){
			alert("只能选择一条记录修改");
			$("#modifyElementModal").modal("hide");
			return;
		}
		var pageId = "";
		var elementId = "";
		$.each($inputs,function(index,$input){
			 pageId = $($input).attr("name");
			 elementId = $($input).attr("id");
		});*/
		var pageId = tree.index.moduleTree.pageIdToEle;
		var elementId = tree.index.moduleTree.elementId;
		//获取修改的元素参数
		//var pageId = tree.index.moduleTree.pageId;modifyElementType
		var elementTypeId = $("#modifyElementType").find("option:selected").val();
		var elementTypeName = $("#modifyElementType").find("option:selected").text();
		var elementContent = $("#modifyElementContent").val();	
		var elementOrder = $("#modifyElementOrder").val();	
		var elementActionId = $("#modifyElementAction").find("option:selected").val();
		//判断选择的操作类型 如果非input框，则把input框中填写的内容先清空
		var elementActionName = $("#modifyElementAction").find("option:selected").text();
		if(elementActionName=="input"||elementActionName=="focusClick"){
			$("#modifyElementText").val();
		}else{
			$("#modifyElementText").val("");
			$("#modifyElementFocusType ").get(0).options[0].selected = true;  
		}
		if(elementActionId!="3"&&elementActionId!="4"&&elementActionId!="5"&&elementActionId!="6"){
			$("#modifyElementDrag").val(0);
		}
		var elementText = $("#modifyElementText").val();
		var modifyRepeatContentFlag = $("#modifyRepeatContentFlag").prop("checked");//输入框信息是否需要添加随机后缀
		var elementTextFlag = "N";
		if(modifyRepeatContentFlag==true){
			elementTextFlag = "Y";
		}
		var elementDrag = $("#modifyElementDrag").val();
		var elementPrecious = $("#modifyElementPrecious").find("option:selected").val();
		var elementExpectName = $("#modifyElementExpectName").find("option:selected").val();
		var elementExpectType = $("#modifyElementExpectType").find("option:selected").val();
		var focusTypeId= $("#modifyElementFocusType").find("option:selected").val();
		var focusTypeName= $("#modifyElementFocusType").find("option:selected").text();
		var elementExpectOrder = $("#modifyElementExpectOrder").val();
		var elementExpectContent = $("#modifyElementExpectContent").val();
		var elementExpectResult = $("#modifyElementExpectResult").val();
		var elementOperateDesc = $("#modifyElementOperateDesc").val();
		var elementWaitTime = $("#modifyElementWaitTime").val();
		//获取iframe的信息
		var isIFrameFlag = $(':radio[name="modifyHasIFrame"]:checked').val();
		var isSwitchPage = $(':radio[name="modifySwitchPage"]:checked').val();
		var overIFrameFlag = $(':radio[name="modifyOverIFrame"]:checked').val();
		var iFrameElementType = "";
		var iFrameElementContent = "";
		if(isIFrameFlag==2){
			iFrameElementType = $("#modifyIFrameElementType").find("option:selected").text();//iframe元素类型
			iFrameElementContent = $("#modifyIFrameElementContent").val();//元素文本
		}
		var params = {
			"pageId":pageId,
			"elementId":elementId,
			"elementTypeId":elementTypeId,
			"elementTypeName":elementTypeName,
			"elementContent":elementContent,
			"elementOrder":elementOrder,
			"elementActionId":elementActionId,
			"elementText":elementText,
			"elementTextFlag":elementTextFlag,
			"elementDrag":elementDrag,
			"elementPrecious":elementPrecious,
			"elementWaitTime":elementWaitTime,
			"elementExpectName":elementExpectName,
			"elementExpectType":elementExpectType,
			"elementExpectContent":elementExpectContent,
			"elementExpectOrder":elementExpectOrder,
			"elementExpectResult":elementExpectResult,
			"elementOperateDesc":elementOperateDesc,
			"isIFrameFlag":isIFrameFlag,
			"overIFrameFlag":overIFrameFlag,
			"iFrameElementType":iFrameElementType,
			"iFrameElementContent":iFrameElementContent,
			"isSwitchPage":isSwitchPage,
			"focusTypeId":focusTypeId,
			"focusTypeName":focusTypeName
		};
		var path = contextPath+"/element/modifyElement";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.code==0){
					Lobibox.alert('success', {
			            msg: "修改元素信息成功"
			        });
					//将删除框隐藏
					$("#modifyElementModal").modal("hide");
					//重新刷新元素展示页记录
					tree.index.showElements("",tree.index.moduleTree.pageId);
				}else if(response.code=="-2"){
					alert("对不起，请重新登录！");
					$("#modifyElementModal").modal("hide");
					window.location.href = contextPath + "/user/index";
				}else{
					Lobibox.alert('error', {
	                    msg: "修改页面元素失败"
	                });
					$("#modifyElementModal").modal("hide");
				}
			},error:function(response){
		        Lobibox.alert('error', {
                    msg: "修改页面元素异常"
                });
		        $("#modifyElementModal").modal("hide");
		    }
		});
	};
	//新增页面元素
	var _addElement = function(){
		
		var pageId = tree.index.moduleTree.pageId;
		var elementTypeId = $("#addElementType").find("option:selected").val();
		var elementText = $("#addElementText").val();
		var elementContent = $("#addElementContent").val();	
		if(elementContent!=null&&elementContent!=""&&(elementTypeId==null||elementTypeId=="")){
			alert("请输入元素类型");
			return;
		}
		var elementTypeName = $("#addElementType").find("option:selected").text();
		var elementOrder = $("#addElementOrder").val();	
		var elementActionId = $("#addElementAction").find("option:selected").val();
		if(elementContent!=null&&elementContent!=""&&(elementActionId==null||elementActionId=="")){
			alert("请输入操作类型");
			return;
		}
		var elementText = $("#addElementText").val();
		var addRepeatContentFlag = $("#addRepeatContentFlag").prop("checked");//输入框信息是否需要添加随机后缀
		var elementTextFlag = "N";
		if(addRepeatContentFlag==true){
			elementTextFlag = "Y";
		}
		var elementDrag = $("#addElementDrag").val();	
		var elementPrecious = $("#addElementPrecious").find("option:selected").val();
		var elementExpectName = $("#addElementExpectName").find("option:selected").val();
		var elementExpectType = $("#addElementExpectType").find("option:selected").val();
		var focusTypeId = $("#addElementFocusType").find("option:selected").val();
		var focusTypeName = $("#addElementFocusType").find("option:selected").text();
		var elementExpectOrder = $("#addElementExpectOrder").val();
		var elementWaitTime = $("#addElementWaitTime").val();
		var elementExpectContent = $("#addElementExpectContent").val();
		var elementExpectResult = $("#addElementExpectResult").val();
		var elementOperateDesc = $("#addElementOperateDesc").val();
		//获取iframe的信息
		var isIFrameFlag = $(':radio[name="addHasIFrame"]:checked').val();
		var isSwitchPage = $(':radio[name="switchPage"]:checked').val();//是否切换页面
		var overIFrameFlag = $(':radio[name="addOverHasIFrame"]:checked').val();
		var iFrameElementType = "";
		var iFrameElementContent = "";
		if(isIFrameFlag==2){
			iFrameElementType = $("#iFrameElementType").find("option:selected").val();//iframe元素类型
			iFrameElementContent = $("#iFrameElementContent").val();//元素文本
		}
		var params = {
			"pageId":pageId,
			"elementTypeId":elementTypeId,
			"elementTypeName":elementTypeName,
			"elementContent":elementContent,
			"elementOrder":elementOrder,
			"elementWaitTime":elementWaitTime,
			"elementActionId":elementActionId,
			"elementText":elementText,
			"elementTextFlag":elementTextFlag,
			"elementDrag":elementDrag,
			"elementPrecious":elementPrecious,
			"elementExpectName":elementExpectName,
			"elementExpectType":elementExpectType,
			"elementExpectContent":elementExpectContent,
			"elementExpectOrder":elementExpectOrder,
			"elementExpectResult":elementExpectResult,
			"elementOperateDesc":elementOperateDesc,
			"isIFrameFlag":isIFrameFlag,
			"overIFrameFlag":overIFrameFlag,
			"iFrameElementType":iFrameElementType,
			"iFrameElementContent":iFrameElementContent,
			"isSwitchPage":isSwitchPage,
			"focusTypeId":focusTypeId,
			"focusTypeName":focusTypeName
		};
		var path = contextPath+"/element/addElement";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.code==0){
					Lobibox.alert('success', {
			            msg: "添加元素信息成功"
			        });
					//将删除框隐藏
					$("#addElementModal").modal("hide");
					//重新刷新元素展示页记录
					tree.index.showElements("",tree.index.moduleTree.pageId);
				}else if(response.code=="-2"){
					alert("对不起，请重新登录！");
					$("#addElementModal").modal("hide");
					window.location.href = contextPath + "/user/index";
				}else{
					Lobibox.alert('error', {
	                    msg: "添加页面元素失败"
	                });
					$("#addElementModal").modal("hide");
				}
			},error:function(response){
		        Lobibox.alert('error', {
                    msg: "添加页面元素异常"
                });
		        $("#addElementModal").modal("hide");
		    }
		});
	};
	//插入元素页面初始化
	var _initInsertElement = function(){
		$("#insertElePageName").val(tree.index.moduleTree.pageName);
		//查询元素类型和操作类型，再初始化
		tree.index.queryElementType();
		if(tree.index.moduleTree.projectType==0){
			$("#insertCheckElementType").hide();
			$("#insertWebView").hide();
		}
		var $typeOps = "<option value=''>请选择元素类型</option>";
		var $viewTypeOps = "<option value=''>请选择元素类型</option>";
		var $actionOps = "<option value=''>请选择操作类型</option>";
		var $expectOps = "<option value=''>请选择期待元素类型</option>";
		var $expectTypeOps = "<option value=''>请选择校验的类型</option>";
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elTypeList&&tree.index.moduleTree.eleResult.elTypeList.length>0){
			$.each(tree.index.moduleTree.eleResult.elTypeList,function(index,elList){
				if(elList.element_type_flag==0){
					$typeOps = $typeOps+"<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
				}else{
					$viewTypeOps = $viewTypeOps +"<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
				}
			});
		}
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elActionList&&tree.index.moduleTree.eleResult.elActionList.length>0){
			$.each(tree.index.moduleTree.eleResult.elActionList,function(index,acList){
				$actionOps = $actionOps+"<option value='"+acList.element_action_id+"'>"+acList.element_action_name+"</option>";
			});
		}
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elExpectList&&tree.index.moduleTree.eleResult.elExpectList.length>0){
			$.each(tree.index.moduleTree.eleResult.elExpectList,function(index,ecList){
				$expectOps = $expectOps+"<option value='"+ecList.elementExpectName+"'>"+ecList.elementExpectName+"</option>";
			});
		}
		//校验的类型
		if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.expectTypes&&tree.index.moduleTree.eleResult.expectTypes.length>0){
			$.each(tree.index.moduleTree.eleResult.expectTypes,function(index,type){
				$expectTypeOps = $expectTypeOps+"<option value='"+type.expectTypeName+"'>"+type.expectTypeName+"</option>";
			});
		}
		tree.index.moduleTree.elementType = $typeOps;//普通元素类型
		tree.index.moduleTree.webViewType = $viewTypeOps;//webview类型
		//判断如果是Macaca项目，则初始化macaca元素类型
		if(tree.index.moduleTree.testType==3){
			$("#insertCheckElementType").prop("checked",true);
			$("#insertElementType").html($viewTypeOps);
		}else if(tree.index.moduleTree.testType==4){
			//操作元素类型初始化
			$("#insertCheckElementType").prop("checked",true);
			$("#insertElementType").html($viewTypeOps);
			$("#insertElementFocusType").html($viewTypeOps);
			//iframe定位初始化
			$("#insertMoreIFrame").removeClass("template-hidden");
			$("#insertIFrameElementType").html($viewTypeOps);
		}else{
			$("#insertElementType").html($typeOps);
		}
		$("#insertElementAction").html($actionOps);
		$("#insertElementExpectName").html($expectOps);
		$("#insertElementExpectType").html($expectTypeOps);
		//查询一个页面上的所有元素信息
		var $preEles = "<option value=''>请选择前置元素</option>";;
		var params = {
				"pageId":tree.index.moduleTree.pageId
			};
		var path = contextPath + "/element/queryElementById";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.code=="0"){
					$.each(response.elementList,function(index,list){
						//判断元素id是否已被选择为前置元素
						var elementId = list.element_id;
						var elementContent = list.element_content;//元素的id
						var elementOperateDesc = list.element_operate_desc;
						var i = 0;
						var flag = false;
						while(i<response.elementList.length){
							if(response.elementList[i].element_precious_id==elementId){
								flag = true;
								return;
							}
							i++;
						}
						if(!flag){
							if(elementOperateDesc!=null&&elementOperateDesc!=""){
								$preEles = $preEles + "<option value='"+elementId+"'>"+elementId+"-"+elementOperateDesc+"</option>";
							}else{
								$preEles = $preEles + "<option value='"+elementId+"'>"+elementId+"</option>";
							}
						}
					});
					$("#insertElementPrecious").html($preEles);
				}
	        },error:function(response){
		        Lobibox.alert('error', {
                    msg: "查询元素异常"
                });
		    }
		});
		//webView类型的checkBox事件
		$("#insertCheckElementType").bind("click", function () {
            if($("input[type='checkbox']").is(':checked')){
            	$("#insertElementType").html(tree.index.moduleTree.webViewType);
            }else{
            	$("#insertElementType").html(tree.index.moduleTree.elementType);
            }
        });
		//初始化是否需要切换IFrame
		var iFrameVal = 0;
		$("#insertIsUserIFrame input:radio").click(function(){
			iFrameVal=$(this).val();
    		if(iFrameVal==0){
    			$("#insertIFrameContent").addClass("template-hidden");
    		}else if(iFrameVal==2){
    			//隐藏所有的元素信息
    			$("#insertIFrameContent").removeClass("template-hidden");
    			//$("#moreElementPage").insertClass("element-hidden");
    		}
    	});
		//元素操作类型事件绑定
		$('#insertElementAction').change(function(){ 
			var text = $("#insertElementAction").find("option:selected").text();
			if(text=="input"||text=="dragForView"||text=="select"){
				$("#insertElementTextId").removeClass("elementTextDiv");
				$("#insertElementDragId").addClass("dragElementDiv");
				$("#insertElementFocusTypeId").addClass("elementTextDiv");
			}else if(text=="drag-left"||text=="drag-right"||text=="drag-up"||text=="drag-down"||text.startsWith("scroll")){
				$("#insertElementDragId").removeClass("dragElementDiv");
				$("#insertElementTextId").addClass("elementTextDiv");
				$("#insertElementFocusTypeId").addClass("elementTextDiv");
			}else if(text=="focusClick"){
				$("#insertElementFocusTypeId").removeClass("elementTextDiv");
				$("#insertElementTextId").removeClass("elementTextDiv");
				$("#insertElementDragId").addClass("dragElementDiv");
			}else{
				$("#insertElementFocusTypeId").addClass("elementTextDiv");
				$("#insertElementTextId").addClass("elementTextDiv");
				$("#insertElementDragId").addClass("dragElementDiv");
			};
		});
	};
	//插入页面元素
	var _insertElement = function(){
		
		var pageId = tree.index.moduleTree.pageId;
		var elementTypeId = $("#insertElementType").find("option:selected").val();
		var elementText = $("#insertElementText").val();
		var elementContent = $("#insertElementContent").val();	
		if(elementContent!=null&&elementContent!=""&&(elementTypeId==null||elementTypeId=="")){
			alert("请输入元素类型");
			return;
		}
		var elementTypeName = $("#insertElementType").find("option:selected").text();
		var elementOrder = $("#insertElementOrder").val();	
		var elementActionId = $("#insertElementAction").find("option:selected").val();
		if(elementContent!=null&&elementContent!=""&&(elementActionId==null||elementActionId=="")){
			alert("请输入操作类型");
			return;
		}
		var elementText = $("#insertElementText").val();
		var addRepeatContentFlag = $("#insertRepeatContentFlag").prop("checked");//输入框信息是否需要添加随机后缀
		var elementTextFlag = "N";
		if(addRepeatContentFlag==true){
			elementTextFlag = "Y";
		}
		var elementDrag = $("#insertElementDrag").val();	
		var elementPrecious = $("#insertElementPrecious").val();
		var elementAfter = $("#insertElementAfter").val();
		var elementExpectName = $("#insertElementExpectName").find("option:selected").val();
		var elementExpectType = $("#insertElementExpectType").find("option:selected").val();
		var focusTypeId = $("#insertElementFocusType").find("option:selected").val();
		var focusTypeName = $("#insertElementFocusType").find("option:selected").text();
		var elementExpectOrder = $("#insertElementExpectOrder").val();
		var elementWaitTime = $("#insertElementWaitTime").val();
		var elementExpectContent = $("#insertElementExpectContent").val();
		var elementExpectResult = $("#insertElementExpectResult").val();
		var elementOperateDesc = $("#insertElementOperateDesc").val();
		//获取iframe的信息
		var isIFrameFlag = $(':radio[name="insertOverHasIFrame"]:checked').val();
		var isSwitchPage = $(':radio[name="insertSwitchPage"]:checked').val();//是否切换页面
		var overIFrameFlag = $(':radio[name="insertOverHasIFrame"]:checked').val();
		var iFrameElementType = "";
		var iFrameElementContent = "";
		if(isIFrameFlag==2){
			iFrameElementType = $("#insertIFrameElementType").find("option:selected").val();//iframe元素类型
			iFrameElementContent = $("#insertIFrameElementContent").val();//元素文本
		}
		var params = {
			"pageId":pageId,
			"elementTypeId":elementTypeId,
			"elementTypeName":elementTypeName,
			"elementContent":elementContent,
			"elementOrder":elementOrder,
			"elementWaitTime":elementWaitTime,
			"elementActionId":elementActionId,
			"elementText":elementText,
			"elementTextFlag":elementTextFlag,
			"elementDrag":elementDrag,
			"elementPrecious":elementPrecious,
			"elementAfter":elementAfter,
			"elementExpectName":elementExpectName,
			"elementExpectType":elementExpectType,
			"elementExpectContent":elementExpectContent,
			"elementExpectOrder":elementExpectOrder,
			"elementExpectResult":elementExpectResult,
			"elementOperateDesc":elementOperateDesc,
			"isIFrameFlag":isIFrameFlag,
			"overIFrameFlag":overIFrameFlag,
			"iFrameElementType":iFrameElementType,
			"iFrameElementContent":iFrameElementContent,
			"isSwitchPage":isSwitchPage,
			"focusTypeId":focusTypeId,
			"focusTypeName":focusTypeName
		};
		var path = contextPath+"/element/insertElement";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.code==0){
					Lobibox.alert('success', {
			            msg: "插入元素信息成功"
			        });
					//将删除框隐藏
					$("#insertElementModal").modal("hide");
					//重新刷新元素展示页记录
					tree.index.showElements("",tree.index.moduleTree.pageId);
				}else if(response.code=="-2"){
					alert("对不起，请重新登录！");
					$("#insertElementModal").modal("hide");
					window.location.href = contextPath + "/user/index";
				}else{
					Lobibox.alert('error', {
	                    msg: "插入页面元素失败"
	                });
					$("#addElementModal").modal("hide");
				}
			},error:function(response){
		        Lobibox.alert('error', {
                    msg: "插入页面元素异常"
                });
		        $("#insertElementModal").modal("hide");
		    }
		});
	};
	//新增界面是否展示元素判断（初始化新增界面）
	var _isRootPage = function(pthis){
		if($(pthis)&&$(pthis).parent().parent().find("span a")){
			tree.index.moduleTree.pageId = $(pthis).parent().parent().find("span a").attr("id");
		}
		if($(pthis).attr("id")=="insert"){
			tree.index.moduleTree.pageFlag = $(pthis).attr("id");
		}else{
			tree.index.moduleTree.pageFlag = "";
		}
		//添加页面名称校验事件
		$("#addPageName").blur(function(){
			if(!$("#addPageNameSpan").hasClass("span-hidden")){
				$("#addPageNameSpan").addClass("span-hidden");
				$("#addPageInfoButton").attr("disabled",false);
			}
			var pageName = $("#addPageName").val();
			if(pageName==""||pageName==null){
				//alert("请输入页面名称");
				return;
			}
			var flag = tree.index.checkPageName(pageName);
			if(flag){
				$("#addPageNameSpan").removeClass("span-hidden");
				$("#addPageInfoButton").attr("disabled",true);
			}
		});
		//初始化是否复用模板功能
		var templateVal = 0;
		$("#isUserTemplate input:radio").click(function(){
			templateVal=$(this).val();
    		if(templateVal==0){
    			$("#copyTemplateDiv").addClass("template-hidden");
    			//判断是否是开始节点 是:不展示元素信息;否：展示元素信息
    			if(tree.index.moduleTree.pageId!=""&&tree.index.moduleTree.pageId==0){
    				$("#moreElementPage").addClass("element-hidden");
    			}else{
    				if($("#moreElementPage").hasClass("element-hidden")){
        				$("#moreElementPage").removeClass("element-hidden");
        			}
    			}
    		}else if(templateVal==2){
    			//隐藏所有的元素信息
    			$("#copyTemplateDiv").removeClass("template-hidden");
    			$("#moreElementPage").addClass("element-hidden");
    		}
    	});
		//是否是iFrame操作
		if(tree.index.moduleTree.testType==4){
			$("#moreIFrame").removeClass("template-hidden");
		}else{
			$("#moreIFrame").addClass("template-hidden");
		}
		//初始化是否需要切换IFrame
		var iFrameVal = 0;
		$("#isUserIFrame input:radio").click(function(){
			iFrameVal=$(this).val();
    		if(iFrameVal==0){
    			$("#iFrameContent").addClass("template-hidden");
    		}else if(iFrameVal==2){
    			//隐藏所有的元素信息
    			$("#iFrameContent").removeClass("template-hidden");
    			//$("#moreElementPage").addClass("element-hidden");
    		}
    	});
		
		if(tree.index.moduleTree.pageId!=""&&tree.index.moduleTree.pageId!=0){
			$("#moreElementPage").removeClass("element-hidden");
			//初始化新增页面上的元素类型和操作类型
			tree.index.queryElementType();
			if(tree.index.moduleTree.projectType==0){
				$("[id^='checkElementType']").hide();
				$("#WebView").hide();
			}
			var $typeOps = "<option value=''>请选择元素类型</option>";
			var $viewTypeOps = "<option value=''>请选择元素类型</option>";
			var $actionOps = "<option value=''>请选择操作类型</option>";
			var $xpath = "";
			var $expectOps = "<option value=''>请选择期待元素类型</option>";
			var $expectTypeOps = "<option value=''>请选择校验的类型</option>";
			if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elTypeList&&tree.index.moduleTree.eleResult.elTypeList.length>0){
				$.each(tree.index.moduleTree.eleResult.elTypeList,function(index,elList){
					if(elList.element_type_flag==0){
						$typeOps = $typeOps+"<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
					}else{
						if(elList.element_type_name=="Xpath"){
							$xpath = "<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
						}else{
							$viewTypeOps = $viewTypeOps +"<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
						}
					}
				});
			}
				$viewTypeOps = $viewTypeOps+$xpath;//对元素的排序特殊处理
			if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elActionList&&tree.index.moduleTree.eleResult.elActionList.length>0){
				$.each(tree.index.moduleTree.eleResult.elActionList,function(index,acList){
					$actionOps = $actionOps+"<option value='"+acList.element_action_id+"'>"+acList.element_action_name+"</option>";
				});
			}
			//期待元素类型初始化
			if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elExpectList&&tree.index.moduleTree.eleResult.elExpectList.length>0){
				$.each(tree.index.moduleTree.eleResult.elExpectList,function(index,ecList){
					$expectOps = $expectOps+"<option value='"+ecList.elementExpectName+"'>"+ecList.elementExpectName+"</option>";
				});
			}
			//校验的类型初始化
			if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.expectTypes&&tree.index.moduleTree.eleResult.expectTypes.length>0){
				$.each(tree.index.moduleTree.eleResult.expectTypes,function(index,type){
					$expectTypeOps = $expectTypeOps+"<option value='"+type.expectTypeName+"'>"+type.expectTypeName+"</option>";
				});
			}
			tree.index.moduleTree.elementType = $typeOps;//普通元素类型
			tree.index.moduleTree.webViewType = $viewTypeOps;//webview类型
			
			//判断如果是Macaca项目，则初始化macaca元素类型
			if(tree.index.moduleTree.testType==3){
				$("#checkElementType").prop("checked",true);
				$("#elementType").html($viewTypeOps);
			}else if(tree.index.moduleTree.testType==4){
				$("#elementFocusType").html($viewTypeOps);
				$("#elementType").html($viewTypeOps);
				$("#iFrameElementType").html($viewTypeOps);
			}else{
				$("#elementType").html($typeOps);
			}
			
			$("#elementAction").html($actionOps);
			$("#elementExpectName").html($expectOps);
			$("#elementExpectType").html($expectTypeOps);
			//查询一个页面上的所有元素信息
			var $preEles = "<option value=''>请选择前置元素</option>";
			var params = {
					"pageId":tree.index.moduleTree.pageId
				};
			var path = contextPath + "/element/queryElementById";
			$.ajax({
				type:"POST",
				url:path,
				contentType:"application/json; charset=utf-8",
				data:JSON.stringify(params),
				dataType:"json",
				async:false,
				success: function (response) {
					if(response&&response.code=="0"){
						$.each(response.elementList,function(index,list){
							//判断元素id是否已被选择为前置元素
							var elementId = list.element_id;
							var elementContent = list.element_content;
							var elementOperateDesc = list.element_operate_desc;
							var i = 0;
							var flag = false;
							while(i<response.elementList.length){
								if(response.elementList[i].element_precious_id==elementId){
									flag = true;
									return;
								}
								i++;
							}
							if(!flag){
								if(elementOperateDesc!=""&&elementOperateDesc!=null){
									$preEles = $preEles + "<option value='"+elementId+"'>"+elementId+"-"+elementOperateDesc+"</option>";
								}else{
									$preEles = $preEles + "<option value='"+elementId+"'>"+elementId+"</option>";
								}
							}
						});
						$("#elementPrecious").html($preEles);
					}
		        },error:function(response){
			        alert("查询元素异常");
			    }
			});
			//webView类型的checkBox事件
			$("#checkElementType").bind("click", function () {
                if($("input[type='checkbox']").is(':checked')){
                	$("#elementType").html(tree.index.moduleTree.webViewType);
                }else{
                	$("#elementType").html(tree.index.moduleTree.elementType);
                }
            });
			//元素操作类型事件绑定
			$("#elementAction").change(function(){
				var text = $("#elementAction").find("option:selected").text();
				if(text=="input"||text=="dragForView"||text=="select"){
					$("#elementTextId").removeClass("elementTextDiv");
					$("#elementDragId").addClass("dragElementDiv");
				}else if(text=="drag-left"||text=="drag-right"||text=="drag-up"||text=="drag-down"||text.startsWith("scroll")){
					$("#elementDragId").removeClass("dragElementDiv");
					$("#elementTextId").addClass("elementTextDiv");
				}else if(text=="focusClick"){
					$("#elementTextId").removeClass("elementTextDiv");
					$("#elementFocusTypeId").removeClass("elementTextDiv");
					$("#elementDragId").addClass("dragElementDiv");
				}else{
					$("#elementFocusTypeId").addClass("elementTextDiv");
					$("#elementTextId").addClass("elementTextDiv");
					$("#elementDragId").addClass("dragElementDiv");
				};
			});
		};
	};
	//拖拽根页面时，新增页面
	var _createNewPage = function(param){
		$("#addPage").modal("show");
		tree.index.moduleTree.param = param;
		tree.index.moduleTree.pageId = param.parentPageId;
		var pageName = param.pageName;
		//if(tree.index.moduleTree.pageId!=""&&tree.index.moduleTree.pageId!=0){
			$("#moreElementPage").removeClass("element-hidden");
			//初始化新增页面上的元素类型和操作类型
			tree.index.queryElementType();
			if(tree.index.moduleTree.projectType==0){
				$("[id^='checkElementType']").hide();
				$("#WebView").hide();
			}
			var $typeOps = "";
			var $viewTypeOps = "";
			var $actionOps = "";
			var $expectOps = "<option value=''>请选择期待元素类型</option>";
			var $expectTypeOps = "<option value=''>请选择校验的类型</option>";
			if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elTypeList&&tree.index.moduleTree.eleResult.elTypeList.length>0){
				$.each(tree.index.moduleTree.eleResult.elTypeList,function(index,elList){
					if(elList.element_type_flag==0){
						$typeOps = $typeOps+"<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
					}else{
						$viewTypeOps = $viewTypeOps +"<option value='"+elList.element_type_id+"'>"+elList.element_type_name+"</option>";
					}
				});
			}
			if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elActionList&&tree.index.moduleTree.eleResult.elActionList.length>0){
				$.each(tree.index.moduleTree.eleResult.elActionList,function(index,acList){
					$actionOps = $actionOps+"<option value='"+acList.element_action_id+"'>"+acList.element_action_name+"</option>";
				});
			}
			//期待元素类型初始化
			if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.elExpectList&&tree.index.moduleTree.eleResult.elExpectList.length>0){
				$.each(tree.index.moduleTree.eleResult.elExpectList,function(index,ecList){
					$expectOps = $expectOps+"<option value='"+ecList.elementExpectName+"'>"+ecList.elementExpectName+"</option>";
				});
			}
			//校验的类型
			if(tree.index.moduleTree.eleResult&&tree.index.moduleTree.eleResult.expectTypes&&tree.index.moduleTree.eleResult.expectTypes.length>0){
				$.each(tree.index.moduleTree.eleResult.expectTypes,function(index,type){
					$expectTypeOps = $expectTypeOps+"<option value='"+type.expectTypeName+"'>"+type.expectTypeName+"</option>";
				});
			}
			tree.index.moduleTree.elementType = $typeOps;//普通元素类型
			tree.index.moduleTree.webViewType = $viewTypeOps;//webview类型
			$("#elementType").html($typeOps);
			$("#elementAction").html($actionOps);
			$("#elementExpectName").html($expectOps);
			//查询一个页面上的所有元素信息
			var $preEles = "<option value=''>请选择前置元素</option>";
			var params = {
					"pageId":tree.index.moduleTree.pageId
				};
			var path = contextPath + "/element/queryElementById";
			$.ajax({
				type:"POST",
				url:path,
				contentType:"application/json; charset=utf-8",
				data:JSON.stringify(params),
				dataType:"json",
				async:false,
				success: function (response) {
					if(response&&response.code=="0"){
						$.each(response.elementList,function(index,list){
							//判断元素id是否已被选择为前置元素
							var elementId = list.element_id;
							var elementContent = list.element_content;
							var elementOperateDesc = list.element_operate_desc;
							var i = 0;
							var flag = false;
							while(i<response.elementList.length){
								if(response.elementList[i].element_precious_id==elementId){
									flag = true;
									return;
								}
								i++;
							}
							if(!flag){
								$preEles = $preEles + "<option value='"+elementId+"'>"+elementOperateDesc+"</option>";
							}
						});
						$("#elementPrecious").html($preEles);
					}
		        },error:function(response){
			        alert("查询元素异常");
			    }
			});
			//webView类型的checkBox事件
			$("#checkElementType").bind("click", function () {
                if($("input[type='checkbox']").is(':checked')){
                	$("#elementType").html(tree.index.moduleTree.webViewType);
                }else{
                	$("#elementType").html(tree.index.moduleTree.elementType);
                }
            });
			//元素操作类型事件绑定
			$("#elementAction").change(function(){
				var text = $("#elementAction").find("option:selected").text();
				if(text=="input"){
					$("#elementTextId").removeClass("elementTextDiv");
					$("#elementDragId").addClass("dragElementDiv");
				}else if(text=="drag-left"||text=="drag-right"||text=="drag-up"||text=="drag-down"||text.startsWith("scroll")){
					$("#elementDragId").removeClass("dragElementDiv");
					$("#elementTextId").addClass("elementTextDiv");
				}else{
					$("#elementTextId").addClass("elementTextDiv");
					$("#elementDragId").addClass("dragElementDiv");
				};
			});
			$("#addPageName").val(pageName);
			$("#addPageName").attr("disabled","disabled");
		//};
	}
	//将查询元素类型和元素操作类型独立
	var _queryElementType = function(){
		//获取平台类型
		var testType = $("#testType").val();
		//初始化新增页面上的元素类型和操作类型
		var params = {"testType":testType};
		var path = contextPath + "/element/queryElementType";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				tree.index.moduleTree.eleResult = response;
	        },error:function(response){
		        alert("查询元素类型或元素操作类型异常");
		    }
		});
	};
	//隐藏新增界面中是否要展示的元素信息
	/*var _cancelPage = function(){
		$("#moreElementPage").addClass("element-hidden");
		$("#elementTextId").addClass("elementTextDiv");
		$("#elementDragId").addClass("dragElementDiv");
	};*/
	//新增页面提交
	var _addPageInfo = function(){
		
		var params = {};
		var moduleId = $("#moduleId").val();
		var pageName = $("#addPageName").val();//若选择复用模板，则填写要复用界面的名称;否则填入新增页面的名称
		if(pageName==null||pageName==""){
			alert("请输入页面名称");
			return;
		}
		//获取是否复用模板标识  2:是   0:否
		var isTemplateFlag = $(':radio[name="hasTemplate"]:checked').val();
		var isIFrameFlag = $(':radio[name="hasIFrame"]:checked').val();
		var overIFrameFlag = $(':radio[name="overHasIFrame"]:checked').val();
		var isSwitchPage = $(':radio[name="switchPage"]:checked').val();
		if(isTemplateFlag&&isTemplateFlag!=2){
			//添加页面元素
			var elementTypeId = $("#elementType").find("option:selected").val();//元素类型
			var elementText = $("#elementText").val();//操作类型需要输入的文本信息
			var elementContent = $("#elementContent").val();//元素文本
			if(elementContent!=null&&elementContent!=""&&(elementTypeId==null||elementTypeId=="")){
				alert("请输入元素类型");
				return;
			}
			var elementTypeName = $("#elementType").find("option:selected").text();//元素类型名称
			var elementOrder = $("#elementOrder").val();//元素位置
			var elementWaitTime = $("#elementWaitTime").val();//元素等待时间
			var repeatContentFlag = $("#repeatContentFlag").prop("checked");//输入框信息是否需要添加随机后缀
			var elementTextFlag = "N";
			if(repeatContentFlag==true){
				elementTextFlag = "Y";
			}
			var dragCount = $("#dragCount").val();//拖曳次数
			var elementPrecious = $("#elementPrecious").find("option:selected").val();//是否有前置元素
			var elementActionId = $("#elementAction").find("option:selected").val();//元素操作类型
			if(elementContent!=null&&elementContent!=""&&(elementActionId==null||elementActionId=="")){
				alert("请输入操作类型");
				return;
			}
			var elementActionName = $("#elementAction").find("option:selected").text();//元素操作类型
			var focusTypeId = $("#elementFocusType").find("option:selected").val();//元素操作类型
			var focusTypeName= $("#elementFocusType").find("option:selected").text();
			var elementExpectName = $("#elementExpectName").find("option:selected").val();//期待元素类型
			var elementExpectType = $("#elementExpectType").find("option:selected").val();//期待元素类型
			var elementExpectOrder = $("#elementExpectOrder").val();
			var elementExpectContent = $("#elementExpectContent").val();//操作内容
			var elementExpectResult = $("#elementExpectResult").val();
			if(elementExpectContent.includes("，")||elementExpectResult.includes("，")){
				alert("输入操作内容或期待结果包含 ， 符号，请重新填写");
				return;
			}
			var elementOperateDesc = $("#elementOperateDesc").val();
			var iFrameElementType = "";
			var iFrameElementContent = "";
			if(isIFrameFlag==2){
				iFrameElementType = $("#iFrameElementType").find("option:selected").val();//iframe元素类型
				iFrameElementContent = $("#iFrameElementContent").val();//元素文本
			}
			params = {
				"pageId":tree.index.moduleTree.pageId,
				"pageFlag":tree.index.moduleTree.pageFlag,
				"moduleId":moduleId,
				"pageName":pageName,
				"elementTypeId":elementTypeId,
				"elementTypeName":elementTypeName,
				"elementContent":elementContent,
				"elementOrder":elementOrder,
				"elementWaitTime":elementWaitTime,
				"elementText":elementText,
				"elementTextFlag":elementTextFlag,
				"elementPrecious":elementPrecious,
				"elementActionId":elementActionId,
				"elementActionName":elementActionName,
				"dragCount":dragCount,
				"isTemplateFlag":isTemplateFlag,
				"elementExpectName":elementExpectName,
				"elementExpectType":elementExpectType,
				"elementExpectContent":elementExpectContent,
				"elementExpectOrder":elementExpectOrder,
				"elementExpectResult":elementExpectResult,
				"elementOperateDesc":elementOperateDesc,
				"isIFrameFlag":isIFrameFlag,
				"overIFrameFlag":overIFrameFlag,
				"iFrameElementType":iFrameElementType,
				"iFrameElementContent":iFrameElementContent,
				"isSwitchPage":isSwitchPage,
				"focusTypeId":focusTypeId,
				"focusTypeName":focusTypeName
			};
			if(tree.index.moduleTree.param&&tree.index.moduleTree.param!=""){
				params.dragPageId = tree.index.moduleTree.param.pageId;
				params.nativeParentPageId = tree.index.moduleTree.param.nativeParentPageId;
			}
		}else{
			var templateModuleName = $("#templateModuleName").val();
			var templatePageDepth = $("#templatePageDepth").val();
			params = {
					"isTemplateFlag":isTemplateFlag,
					"pageId":tree.index.moduleTree.pageId,
					"moduleId":moduleId,
					"pageName":pageName,
					"templateModuleName":templateModuleName,
					"templatePageDepth":templatePageDepth
				};
		}
		
		var path = contextPath + "/page/insertPageInfo";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.code==0){
					 Lobibox.alert('success', {
				            msg: "新增页面信息成功"
				        });
						//关闭添加页面
						$("#addPage").modal("hide");
						//重新加载组织结构图
						tree.index.showScriptChart();
				}else if(response.code==-1){
					Lobibox.alert('error', {
			            msg: response.message
			        });
				}else{
					Lobibox.alert('error', {
			            msg: "新增页面信息异常"
			        });
				}
	        },error:function(response){
                Lobibox.alert('error', {
                    msg: "新增页面信息失败"
                });
		    }
		});
	};
	//删除页面初始化
	var _deletePage = function(pthis){
		if($(pthis)&&$(pthis).parent().parent().find("span a")){
			tree.index.moduleTree.pageId = $(pthis).parent().parent().find("span a").attr("id");
		}
	};
	//删除页面提交
	var _deletePageById = function(){
		var moduleId = $("#moduleId").val();
		var params = {
			"pageId":tree.index.moduleTree.pageId,
			"moduleId":moduleId
		};
		var path = contextPath + "/page/deletePageById";
		$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				Lobibox.alert('success', {
		            msg: "删除页面信息成功"
		        });
				//关闭删除页面
				$("#deletePage").modal("hide");
				//重新加载组织结构图
				tree.index.showScriptChart();
	        },error:function(response){
		        alert("新增页面失败");
		        $("#deletePage").modal("hide");
		    }
		});
	};
	var _getParentElementId = function(pageid){
	 	 $("#parentElementId").html("");
	    var html="<option value=0>" +"无"+"</option>";
	 	$.ajax({
		    	type:"post",
		    	url:contextPath+"/element/getParentElement/"+pageid,
		    	data:{},
		    	async: false,
		    	dataType:"json",
		        success: function (data) {
		        	var objs=eval(data);
		        	var obj=objs[0];
		        	var elements=obj.elements;
		       		if(elements.length>0){
			       		for(var i=0;i<elements.length;i++){
			       			html += "<option value='" + elements[i].elementid + "'>" + elements[i].rowNo + "|"+elements[i].classtype+"|"+elements[i].text+"|"+elements[i].elementindex+"</option>";
				       		}
		          	}
		        },error:function(data){
			          html="<option>网络异常</option>";
			        }
		          
		  	});
	 	 $(html).appendTo("#parentElementId");
	   };
	//初始化结构图
	var _initTree = function(){
	    /*var opts = {
	            chartElement : '#chart',
	            dragAndDrop  : true
	        };*/
		$("#chart").html("");
	    jq("#org").jOrgChart({
	    	chartElement : '#chart',
            dragAndDrop  : true
	    });
	       
	        //jq("#org").jOrgChart(opts);
	   };
    var _scroll = function(){
    	  $(".node").click(function(){
    	  $("#chart").scrollTop(0);
    	  $("#chart").scrollTop($(this).offset().top-140);
    	});
    };
    //校验页面名称不可重复
    var _checkPageName = function(pageName){
    	var pageFlag = false;
    	var params = {
    		"pageId":tree.index.moduleTree.pageId,
    		"pageName":pageName,
    		"moduleId":tree.index.moduleTree.tags[1]
    	};
    	var path = contextPath + "/page/checkPageName";
    	$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.pageList){
					if(response.pageList.length>0){
						$.each(response.pageList,function(index,page){
							if(page.page_name==pageName){
								pageFlag = true;
								return false;
							}
						});
					}
				}
	        },error:function(response){
		        alert("校验页面名称失败");
		    }
		});
    	return pageFlag;
    };
    //校验修改页面名称是否合法
    var _checkModifyPageName = function(pageName){
    	var pageFlag = false;
    	var params = {
    		"pageId":tree.index.moduleTree.pageId,
    		"moduleId":tree.index.moduleTree.tags[1],
    		"pageName":pageName
    	};
    	var path = contextPath + "/page/checkModifyPageName";
    	$.ajax({
			type:"POST",
			url:path,
			contentType:"application/json; charset=utf-8",
			data:JSON.stringify(params),
			dataType:"json",
			async:false,
			success: function (response) {
				if(response&&response.pageList){
					if(response.pageList.length>0){
						$.each(response.pageList,function(index,page){
							if(page.page_name==pageName){
								pageFlag = true;
								return false;
							}
						});
					}
				}
	        },error:function(response){
		        alert("校验页面名称失败");
		    }
		});
    	return pageFlag;
    }
	//节点选中并初始化事件
	var _nodeSelected = function(){
		//节点选中事件
		$('#treeview').on('nodeSelected', function(event, data) {
			if(data&&data.href){
				tree.index.moduleTree.href = data.href;
				tree.index.moduleTree.nodeId = data.nodeId;
				//相关数据保存
				if(data.tags!=undefined){
					tree.index.moduleTree.tags = data.tags;
				}else{
					tree.index.moduleTree.tags = [];
				}
				if(tree.index.moduleTree.href=="#child"){
					//添加按钮可用
					$("#addModule").attr("disabled",false);
					$("#modifyModule").attr("disabled",true);
					$("#delModule").attr("disabled",true);
					//将分页的当前页面重新初始化
					tree.index.moduleTree.cpage = 1;
					//tree.index.moduleTree.initFlag==0;//点击模块，需要把标识置成初始化状态0
				}else if(tree.index.moduleTree.href=="#childNode"){
					//删除按钮可用
					$("#delModule").attr("disabled",false);
					$("#modifyModule").attr("disabled",false);
					$("#addModule").attr("disabled",true);
					
					$(".postTitle span").addClass("span-hidden");
	    			//隐藏返回项目信息列表按钮和生成脚本按钮
	    			$("#backToProject").removeClass("span-hidden");
	    			//是否展示生成脚本功能
	    			//if(user.info.userdata.projectRoleId=="3"){
	    				$("#buildScript").removeClass("span-hidden");
	    				$("#downLoadScript").removeClass("span-hidden");
	    			//}
	    			//tree.index.moduleTree.initFlag==0;//返回到脚本结构图，需要把标识置成初始化状态0
	    			//将H1中的信息提示文本修改
		    		$("#cb_post_title_url").text("构建项目结构图");
					//将分页的当前页面重新初始化
					tree.index.moduleTree.cpage = 1;
					//获取当前点击模块名称
					tree.index.moduleTree.moduleName = data.text;
					//加载右边的脚本结构
					tree.index.showScriptChart();
				}else{
					$("#delModule").attr("disabled",true);
					$("#addModule").attr("disabled",true);
					$("#modifyModule").attr("disabled",true);
					//将分页的当前页面重新初始化
					tree.index.moduleTree.cpage = 1;
				}
			}
		});
		$('#treeview').on('nodeUnselected', function(event, data) {
			 //按钮不可用disabled
			$("#delModule").attr("disabled",true);
			$("#addModule").attr("disabled",true);
		});
		//初始化返回到项目列表界面事件
		$("#backToProject").bind("click",function(){
			window.location.href = contextPath + "/user/home";
		});
	};
	//初始化生成脚本页面
	var _initBuildScript = function(projectId){
		//$('#buildScriptModal').modal({backdrop: 'static', keyboard: false});
		var ops = "<option selected value=''>全部</option>";
		var params = {
	    		"projectId":projectId
	    	};
	    	var path = contextPath + "/project/queryModels";
	    	$.ajax({
				type:"POST",
				url:path,
				contentType:"application/json; charset=utf-8",
				data:JSON.stringify(params),
				dataType:"json",
				async:false,
				success: function (response) {
					if(response&&response.modulesList){
						if(response.modulesList.length>0){
							$.each(response.modulesList,function(index,module){
								if(tree.index.moduleTree.moduleName&&tree.index.moduleTree.moduleName!=""&&tree.index.moduleTree.moduleName==module.moduleName){
									ops = ops+"<option selected value='"+module.moduleId+"'>"+module.moduleName+"</option>";
								}else{
									ops = ops+"<option value='"+module.moduleId+"'>"+module.moduleName+"</option>";
								}
							});
						}
					}
		        },error:function(response){
			        alert("查询项目模块失败");
			    }
			});
	    	$("#buildScriptModel").html(ops);
	}
	return{
		editTreeById:_editTreeById,
		modifyModuleInfo:_modifyModuleInfo,
		deleteModuleById:_deleteModuleById,
		insertModule:_insertModule,
		loadProjectScript:_loadProjectScript,
		moduleTree:_moduleTree,
		nodeSelected:_nodeSelected,
		showScriptChart:_showScriptChart,
		initTree:_initTree,
		scroll:_scroll,
		getParentElementId:_getParentElementId,
		initModifyPage:_initModifyPage,
		modifyPage:_modifyPage,
		showElements:_showElements,
		isRootPage:_isRootPage,
		queryElementType:_queryElementType,
		addPageInfo:_addPageInfo,
		/*cancelPage:_cancelPage,*/
		deletePageById:_deletePageById,
		deletePage:_deletePage,
		initModifyElement:_initModifyElement,
		initAddElement:_initAddElement,
		delElement:_delElement,
		modifyElement:_modifyElement,
		addElement:_addElement,
		insertElement:_insertElement,
		initInsertElement:_initInsertElement,
		buildScript:_buildScript,
		downLoadScript:_downLoadScript,
		checkPageName:_checkPageName,
		checkModifyPageName:_checkModifyPageName,
		modifyElementType:_modifyElementType,
		initDelElement:_initDelElement,
		initAddModuleModal:_initAddModuleModal,
		initBuildScript:_initBuildScript,
		createNewPage:_createNewPage,
		initModifyModuleModal:_initModifyModuleModal
	};
})();
$(function(){
	//初始化脚本树
	tree.index.editTreeById();
	tree.index.nodeSelected();
	//显示登录用户信息
	$("#user-info").show();
	$("#adminUserName").text(user.info.userdata.userName);
	//modal框隐藏事件
	$("#addPage").on("hide.bs.modal",function(e){
		$(this).removeData();//清空缓存数据
		 //清空填单内容
		$("#addPageName").val("");
		$("#elementContent").val("");
		$("#elementOrder").val("");
		$("#elementDrag").val("");
		$("#elementPrecious").val("");
		$("#elementOperateDesc").val("");
		$("#elementExpectContent").val("");
		$("#elementExpectOrder").val("");
		$("#elementExpectResult").val("");
		$("#elementText").val("");
		$("#moreElementPage").addClass("element-hidden");
		$("#elementTextId").addClass("elementTextDiv");
		$("#elementDragId").addClass("dragElementDiv");
		$("#copyTemplateDiv").addClass("template-hidden");
		var isTemplateFlag = $(':radio[name="hasTemplate"]:checked').val();
		//$('input:radio[name="sex"]').is(":checked");
		if(isTemplateFlag==2){
			$(':radio[name="hasTemplate"]:checked').removeAttr("checked");
			$("#notTemplateRadioId").prop("checked",true);
		}
		//判断是否存在disabled的input框，是：重新加载
		if($("#addPageName").attr("disabled")=="disabled"){
			//重新加载组织结构图
			$("#addPageName").attr("disabled",false);
			tree.index.moduleTree.param = "";//清空拖拽时传递的参数信息
			tree.index.moduleTree.pageId = "";
			tree.index.showScriptChart();
		}
	});
});